package com.fyh.service.impl;

import com.fyh.constant.MessageConstant;
import com.fyh.entity.common.Result;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.job.JobPosition;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.SimpleCandidate;
import com.fyh.entity.user.Vitae;
import com.fyh.feign.EmployerCandidateFeignClient;
import com.fyh.feign.EmployerChatFeignClient;
import com.fyh.feign.EmployerJobFeignClient;
import com.fyh.feign.EmployerUserFeignClient;
import com.fyh.service.EmployerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployerServiceImpl implements EmployerService {

    @Qualifier("com.fyh.feign.EmployerJobFeignClient")
    @Autowired
    EmployerJobFeignClient employerJobFeignClient;

    @Qualifier("com.fyh.feign.EmployerUserFeignClient")
    @Autowired
    EmployerUserFeignClient employerUserFeignClient;

    @Qualifier("com.fyh.feign.EmployerCandidateFeignClient")
    @Autowired
    EmployerCandidateFeignClient employerCandidateFeignClient;

    @Qualifier("com.fyh.feign.EmployerChatFeignClient")
    @Autowired
    EmployerChatFeignClient employerChatFeignClient;

    /**
     * 获取职位列表
     *
     * @param employerId
     * @return
     */
    @Override
    public List<SimpleJobPosition> getSimpleJobPositionListByEmployerId(Long employerId) {
        Result result = employerJobFeignClient.getSimpleJobListByEmployerId(employerId);
        if(result == null){
            return null;
        }
        List<SimpleJobPosition> simpleJobList = (List<SimpleJobPosition>) result.getData();
        Result<String> userResult = employerUserFeignClient.getNickname(employerId);
        for(SimpleJobPosition simpleJob :simpleJobList){
            //设置用户名称
            simpleJob.setNickName(userResult.getData());
            //获取标签列表
            List<String> tagList = employerJobFeignClient.getTagNameListByPositionId(simpleJob.getPositionId()).getData();
            simpleJob.setTagList(tagList);
        }
        return simpleJobList;
    }

    /**
     * 获取职位详情
     * @param positionId
     * @return
     */
    @Override
    @Transactional
    public JobPosition getJobPositionDetail(Long positionId) {
        //获取职位详情
        JobPosition jobPosition = employerJobFeignClient.getJobPositionDetail(positionId).getData();

        return jobPosition;
    }

    /**
     * 更新职位信息
     * @param jobPosition
     */
    @Override
    public void updateJobPosition(JobPosition jobPosition) {
        jobPosition.setUpdateTime(LocalDateTime.now());
        employerJobFeignClient.updateJobPosition(jobPosition);
    }

    /**
     * 新增职位
     * @param jobPosition
     */
    @Override
    @Transactional
    public void addJobPosition(JobPosition jobPosition) {
        jobPosition.setCreateTime(LocalDateTime.now());
        jobPosition.setUpdateTime(LocalDateTime.now());
        //新增职位
        employerJobFeignClient.addJobPosition(jobPosition);
    }

    /**
     * 删除职位
     * @param positionId
     * @return
     */
    @Override
    @Transactional
    public void deleteJobPosition(Long positionId) {
        employerJobFeignClient.deleteJobPosition(positionId);
    }

    /**
     * 更新职位状态
     * @param positionId
     */
    @Override
    public void updateJobStatus(Integer status, Long positionId) {
        employerJobFeignClient.updateJobStatus(status, positionId);
    }

    /**
     * 获取职位下所有求职者
     * @param positionId
     * @return
     */
    @Override
    public List<SimpleCandidate> getSimpleCandidateList(Long positionId, String status) {
        // 获取职位下所有的求职者ID及投递时间
        Result<List<DeliveryRecord>> deliveryRecordList = employerJobFeignClient.getSimpleDeliveryRecordListList(positionId, status);
        // 获取通过ID所有求职者基础信息
        List<SimpleCandidate> simpleCandidateList = employerUserFeignClient.getSimpleCandidateList(deliveryRecordList.getData()).getData();
        return simpleCandidateList;
    }

    /**
     * 获取简历详情
     *
     * @param candidateId
     * @return
     */
    @Override
    public Vitae getVitaeDetail(Long candidateId) {
        // 获取简历详情
        Result<Vitae> vitaeResult = employerCandidateFeignClient.getUserVitae(candidateId);
        // 获取用户信息
        Result<SimpleCandidate> candidateResult = employerUserFeignClient.getSimpleCandidate(candidateId);
        // 设置用户技能标签
        Vitae vitae = vitaeResult.getData();
        BeanUtils.copyProperties(candidateResult.getData(), vitae);
        vitae.setTags(employerCandidateFeignClient.getTagNameListByUserId(candidateId).getData());
        return vitae;
    }

    /**
     * 更新投递状态
     * @param deliveryRecord
     */
    @Override
    @Transactional
    public void updateDeliveryStatus(DeliveryRecord deliveryRecord) {
        //保存到数据库
        deliveryRecord.setUpdateTime(LocalDateTime.now());
        employerJobFeignClient.updateDeliveryStatus(deliveryRecord);
        //向投递者发送通知
        employerChatFeignClient.sendDeliveryStatusInform(deliveryRecord);
    }

    /**
     * 新增聊天联系人
     * @param userId
     * @param contactId
     */
    @Override
    public void addChatContact(Long userId, Long contactId) {
        employerChatFeignClient.addChatContact(contactId, userId);
    }
}
